//
//  NewVisitViewController.m
//  Marketing
//
//  Created by wmm on 16/3/3.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "NewVisitViewController.h"
#import "DetailClientCell.h"

#define STARTX [UIView getWidth:20]
#define MYCELLHEIGHT 60

@interface NewVisitViewController ()<UITextFieldDelegate>
{
    NSArray    *_basicArray;
    NSArray    *_placeholderArray;
    UIScrollView * scrollView;
}
@property(nonatomic,strong)UILabel        *visitDateLbl;
@property(nonatomic,strong)UITextField    *companyFld;
@property(nonatomic,strong)UITextField    *visitObjectFld;
@property(nonatomic,strong)UITextField    *visitTypeFld;
@property(nonatomic,strong)UITextField    *visitAddressFld;
@property(nonatomic)BOOL    isRemind;

@property(nonatomic,strong)UIButton       *levelBtn;
@property(nonatomic,strong)UIButton       *fromBtn;
@property(nonatomic,strong)UIButton       *statusBtn;
@property(nonatomic,strong)UISwitch       *switchBtn;


@end

@implementation NewVisitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.hidesBackButton = YES;
    self.title = @"新建拜访";
    
    self.navigationItem.leftBarButtonItem=[ViewTool getBarButtonItemWithTarget:self WithAction:@selector(goToBack)];
    _basicArray = @[@"拜访日期",@"公司名称",@"访问对象",@"拜访类型",@"预约地址"];
    _placeholderArray = @[@"",@"请添加公司",@"请添加人员",@"请选择类型",@"请输入拜访地址"];
    
        [self createUI];
    
    [UIView getHeight:60];
    
    UIView *lineView =[[UIView alloc]initWithFrame:CGRectMake(STARTX, MYCELLHEIGHT - 1, KSCreenW -2*STARTX, 1)];
    lineView.backgroundColor = grayLineColor;
    //[cell.contentView addSubview:lineView];
    
}
- (void)createUI{
    
    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, KSCreenH - [UIView getHeight:50])];
    scrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:scrollView];
    
    UIButton *referBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, KSCreenH - [UIView getHeight:50.0f], KSCreenW, [UIView getHeight:50.0f])];
    referBtn.layer.borderWidth = 0.5;
    referBtn.layer.borderColor = grayLineColor.CGColor;
    referBtn.backgroundColor = graySectionColor;
    [referBtn setTitle:@"确认提交" forState:UIControlStateNormal];
    [referBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [referBtn addTarget:self action:@selector(referNewClient:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:referBtn];
    
    
    [self initSectionOne];
    
}
- (void)initSectionOne{
    CGFloat labelH = 20;
    CGFloat tfH    = 30;
    CGFloat tfY    = 5+ labelH;
    CGFloat tfW    = [UIView getWidth:200];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = grayLineColor;
    [scrollView addSubview:lineView];
    
    for (int i = 0; i<_basicArray.count; i++) {
        UILabel *label = [ViewTool getLabelWith:CGRectMake(STARTX,  10+ MYCELLHEIGHT*i, KSCreenW, labelH) WithTitle:_basicArray[i] WithFontSize:16.0f WithTitleColor:grayFontColor WithTextAlignment:NSTextAlignmentLeft];
        [scrollView addSubview:label];
        
        UIView *line = [ViewTool getLineViewWith:CGRectMake(STARTX, (MYCELLHEIGHT -1)*(i+1) , KSCreenW - 2*STARTX, 1) withBackgroudColor:grayLineColor];
        [scrollView addSubview:line];
    }
    
    _visitDateLbl = [self addLableWithFrame:CGRectMake(STARTX, 30, tfW, tfH) AndStr:@""  AndTextColor:[UIColor lightGrayColor]];
//    _visitDateLbl.backgroundColor = [UIColor redColor];
    [scrollView addSubview:_visitDateLbl];

    UIButton *dateBtn = [self createButtonWithFrame:CGRectMake(KSCreenW-STARTX-30, 30, 25, 25) WithImage:@"calendar.png" WithTag:1];
    [scrollView addSubview:dateBtn];
    
    _companyFld = [self addTextFieldWithFrame:CGRectMake(STARTX, _visitDateLbl.maxY + tfY +5 , tfW, tfH) AndStr:@"请添加公司"  AndTextColor:[UIColor lightGrayColor]];
//    _companyFld.backgroundColor = [UIColor redColor];
    [scrollView addSubview:_companyFld];
    
    UIButton *companyBtn = [self createButtonWithFrame:CGRectMake(KSCreenW-STARTX-30, _visitDateLbl.maxY + tfY+5, 25, 25) WithImage:@"company.png" WithTag:1];
    [scrollView addSubview:companyBtn];
    
    
    _visitObjectFld = [self addTextFieldWithFrame:CGRectMake(STARTX, _companyFld.maxY + tfY+5, tfW, tfH) AndStr:@"请添加人员"  AndTextColor:[UIColor lightGrayColor]];
    [scrollView addSubview:_visitObjectFld];
    
    _visitTypeFld = [self addTextFieldWithFrame:CGRectMake(STARTX, _visitObjectFld.maxY + tfY +5, tfW, tfH) AndStr:@"请选择人员"  AndTextColor:[UIColor lightGrayColor]];
    [scrollView addSubview:_visitTypeFld];
    
    UIButton *typeBtn = [self createButtonWithFrame:CGRectMake(KSCreenW-STARTX-30, _visitObjectFld.maxY + tfY+5, 25, 25) WithImage:@"type.png" WithTag:1];
    [scrollView addSubview:typeBtn];

    _visitAddressFld = [self addTextFieldWithFrame:CGRectMake(STARTX, _visitTypeFld.maxY + tfY+5, tfW, tfH) AndStr:@"请输入拜访地址"  AndTextColor:[UIColor lightGrayColor]];
    [scrollView addSubview:_visitAddressFld];

    UILabel *remindLabel = [ViewTool getLabelWith:CGRectMake(STARTX, _visitAddressFld.maxY + 10, tfW, labelH) WithTitle:@"预约提醒" WithFontSize:16.0f WithTitleColor:grayFontColor WithTextAlignment:NSTextAlignmentLeft];
    [scrollView addSubview:remindLabel];
    
    UIView *line = [ViewTool getLineViewWith:CGRectMake(STARTX, _visitAddressFld.maxY + 10+ tfY +10, KSCreenW - 2*STARTX, 1) withBackgroudColor:grayLineColor];
    [scrollView addSubview:line];
    
    
    _switchBtn =[[UISwitch alloc]init];
    _switchBtn.frame=CGRectMake(KSCreenW-STARTX-40, _visitAddressFld.maxY + 10, 35, 10);
//    _switchBtn.backgroundColor=[UIColor redColor];
    _switchBtn.onTintColor=blueFontColor;
    _switchBtn.tintColor=blueFontColor;
    _switchBtn.thumbTintColor=[UIColor whiteColor];

    [_switchBtn setOn:YES animated:YES];
    
    [_switchBtn addTarget:self action:@selector(getValue1:) forControlEvents:UIControlEventValueChanged];

    if (_switchBtn.isOn) {
        NSLog(@"It is On");
    }
    _isRemind = YES;
    [scrollView addSubview:_switchBtn];
    
}

- (void)getValue1:(UISwitch *)switchBtn{
    if (_isRemind) {
        _switchBtn.onTintColor=blueFontColor;
//        _switchBtn.tintColor=[UIColor whiteColor];
        _switchBtn.thumbTintColor=[UIColor whiteColor];
            _isRemind = NO;
    }else{
            _isRemind = YES;
//        _switchBtn.onTintColor=blueFontColor;
//        _switchBtn.tintColor=blueFontColor;
        NSLog(@",,,,");
        _switchBtn.thumbTintColor=blueFontColor;
    }
    
}

- (void)clickToSelect:(UIButton *)btn{
    if (btn.tag == 1) {
        NSLog(@"date");
    }else if (btn.tag == 2){
        NSLog(@"company");
    }
    
}
//提交按钮
- (void)referNewClient:(UIButton *)btn{
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
-(UITextField *)addTextFieldWithFrame:(CGRect)frame AndStr:(NSString *)placeholder AndTextColor:(UIColor *)color
{
    UITextField *textF=[[UITextField alloc]initWithFrame:frame];
    textF.userInteractionEnabled = YES;
    textF.textColor = color;
    textF.placeholder=placeholder;
    textF.delegate = self;
    return textF;
}

-(UILabel *)addLableWithFrame:(CGRect)frame AndStr:(NSString *)placeholder AndTextColor:(UIColor *)color
{
    UILabel *textL=[[UILabel alloc]initWithFrame:frame];
    textL.userInteractionEnabled = YES;
    textL.textColor = color;
    return textL;
}

- (UIButton *)createButtonWithFrame:(CGRect)frame WithTitle:(NSString *)string WithFont:(CGFloat)font WithTag:(CGFloat)tag WithColor:(UIColor *)color
{
    UIButton *btn = [[UIButton alloc]initWithFrame:frame];
    [btn setTitle:string forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:font];
    [btn setTitleColor:color forState:UIControlStateNormal];
    btn.tag = tag;
    [btn addTarget:self action:@selector(clickToSelect:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

- (UIButton *)createButtonWithFrame:(CGRect)frame WithImage:(NSString *)imgName WithTag:(CGFloat)tag
{
    UIButton *btn = [[UIButton alloc]initWithFrame:frame];
    [btn setImage:[UIImage imageNamed:imgName ] forState:UIControlStateNormal];
    btn.tag = tag;
    [btn addTarget:self action:@selector(clickToSelect:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

- (void)goToBack{
    [self.navigationController popViewControllerAnimated:NO];
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
